// pages/coupon/coupon.js
import {
  getMyCouponInfo,
  saveCouponInfo
} from '../../api/coupon'
import {
  getSharePng
} from '../../config'
import {
  getStorageItem,
  setStorageItem
} from '../../utils/storage'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ruleVisible: false,
    isGainPttqCoupon: '0',
    pptQVisible: false, // 领取普天同庆劵
    glory_size: 0,
    glory_val: 0,
    one_glory_coupon_val: 21,
    helperList: [],
    helper_val: 0,
    helper_size: 0,
    one_helper_coupon_val: 12,
    friend_val: 0,
    friend_size: 0,
    one_friend_coupon_val: 12,
    friend_remanin: 0,
    couponType: '',
    couponSize: '',
    canChargeType: '',
    token: getStorageItem('token'),
    outAnim: false,
    outAnim2: false,
    outAnim3: false,
    outAnim4: false,
    introVisible: false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {


  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
 
    if (!getApp().globalData.token) {
      // getApp().initUserInfo();
      return;
    }
    this.setData({
      token: getStorageItem('token'),

    })
    this.getMyCouponInfo(true)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    const danmuDom = this.selectComponent('#danmuDom')
    if (!danmuDom) {
      return;
    }
    
    // clearStorageItem('needReturnPage')
    danmuDom.endLoop();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
    return {
      title: `${ getStorageItem('user_name')}邀请您为他助力`,
      path: `/pages/help/help?help=${getStorageItem('id')}&name=${ getStorageItem('user_name')}`,
      imageUrl: getSharePng('pttq')
    }
  },
  getMyCouponInfo(once) {
    getMyCouponInfo().then(res => {
      
      const {
        isGainPttqCoupon,
        glory_val,
        glory_size,
        one_glory_coupon_val,
        helperList
      } = res.data || {}
      const {
        helper_val,
        helper_size,
        one_helper_coupon_val
      } = res.data || {}
      const {
        friend_val,
        friend_size,
        one_friend_coupon_val
      } = res.data || {}
      let friend_remanin = one_friend_coupon_val;
      if (friend_val - friend_size * one_friend_coupon_val > 0) {
        friend_remanin = one_friend_coupon_val - (friend_val - friend_size * one_friend_coupon_val)
      }
      

      this.setData({
        isGainPttqCoupon: isGainPttqCoupon,
        pptQVisible: isGainPttqCoupon === '0',
        glory_size,
        glory_val,
        one_glory_coupon_val,
        helperList,
        helper_val,
        helper_size,
        one_helper_coupon_val,
        friend_val,
        friend_size,
        one_friend_coupon_val,
        friend_remanin,

      })
      if (once) {
        this.checkVisibleBySize()
      }

    }).catch(err => {
      
    })
  },
  closeRule() {
    this.setData({
      ruleVisible: false
    })
  },
  showRule() {
    this.setData({
      ruleVisible: true
    })
  },
  getPttqCoupon() {
    this.setData({
      pptQVisible: true,
    })
  },
  hideCoupon1() {


    // return ;
    this.savePTTQCouponInfo()
  },
  /**
   * 领取普天同庆劵
   */
  savePTTQCouponInfo() {
    saveCouponInfo({
        type: '1'
      }).then(res => {
        
        this.setData({
          outAnim: true
        })
        setTimeout(() => {
          this.setData({
            pptQVisible: false,
            outAnim: false
          })
          this.getMyCouponInfo(true);
        }, 300)

      })
      .catch(err => {
        
      })
  },
  chargeCoupon(e) {
    
    const couponType = e.currentTarget.dataset.type
    saveCouponInfo({
        type: couponType
      }).then(res => {
        
        wx.showToast({
          title: '兑换成功',
          icon: 'none'
        })
        this.setData({
          couponType,
          couponSize: res.data.size
        })


        this.getMyCouponInfo();
      })
      .catch(err => {
        wx.showToast({
          title: '兑换失败',
          icon: 'none'
        })
        
      })

  },
  chargeNextCoupon(e) {
    const nextCouponType = this.data.canChargeType


    
    const couponType = e.currentTarget.dataset.type
    saveCouponInfo({
        type: couponType
      }).then(res => {
        
        wx.showToast({
          title: '兑换成功',
          icon: 'none'
        })
        this.setData({
          outAnim3: true
        })
        setTimeout(() => {
          this.setData({
            canChargeType: '',
            // couponType: nextCouponType,
            outAnim3: false

          })
          this.getMyCouponInfo();
        }, 300)
      })
      .catch(err => {
        wx.showToast({
          title: '兑换失败',
          icon: 'none'
        })
        
      })

  },

  hideCouponType(e) {
    const {
      action
    } = e.currentTarget.dataset
    this.setData({
      outAnim2: true
    })
    setTimeout(() => {

      this.setData({
        couponType: '',
        couponSize: '',
        outAnim2: false

      })
      if (action === 'sure') {
        this.checkVisibleBySize();
      }

    }, 300)



  },

  checkVisibleBySize() {
    if (this.data.isGainPttqCoupon === '0') {
      return;
    }
    if (this.data.couponType) {
      return
    }
    // 检查是否有未领取的功勋劵
    if (this.data.glory_size > 0) {
      this.setData({
        canChargeType: '2',
        couponSize: this.data.glory_size,
      })
      return
    }

    // 检查是否有未领取的好友助力劵
    if (this.data.helper_size > 0) {
      this.setData({
        canChargeType: '3',
        couponSize: this.data.helper_size,
      })
      return
    }

    // 检查是否有未领取的助力好友劵
    if (this.data.friend_size > 0) {
      this.setData({
        canChargeType: '4',
        couponSize: this.data.friend_size,
      })
      return
    }


  },
  closeCheckVisible() {
    const couponType = this.data.canChargeType
    this.setData({
      outAnim3: true
    })
    setTimeout(() => {
      this.setData({
        canChargeType: '',
        // couponType: nextCouponType,
        outAnim3: false

      })
      this.getMyCouponInfo();
    }, 300)
    // this.checkVisibleBySize()
  },


  showIntro(){
    this.setData({
      introVisible: true,

    })
  },
  closeIntro(){
    this.setData({
      outAnim4: true
    })
    setTimeout(() => {
      this.setData({
        introVisible: false,
        // couponType: nextCouponType,
        outAnim4: false

      })
      // this.getMyCouponInfo(true);
    }, 300)
  }




})